/*
* ExaminationServiceImpl.java
* Created on  2014-8-4 上午9:39
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-8-4       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.task.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.task.dao.ExaminationDao;
import com.genghis.ptas.task.entity.ExaminationInfo;
import com.genghis.ptas.task.entity.ExaminationTask;
import com.genghis.ptas.task.entity.LibraryTask;
import com.genghis.ptas.task.service.ExaminationService;
import com.genghis.ptas.task.service.LibraryService;
import com.genghis.ptas.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Transactional
@Service("examinationService")
public class ExaminationServiceImpl implements ExaminationService {
    @Autowired
    private ExaminationDao examinationDao;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private LibraryService libraryService;

    @Override
    public Page<ExaminationInfo> findExaminationPageList(PageParameter pageParameter, ExaminationInfo libraryTask) {
        Page<ExaminationInfo> examinationInfoPage = examinationDao.findExaminationPageList(pageParameter, libraryTask);
        for(ExaminationInfo examinationInfo : examinationInfoPage.getResultList()){
            examinationInfo.setShowExaminationType(classifyService.findTypeValuesById(examinationInfo.getExaminationType()).getTypeValue());
        }
        return examinationInfoPage;
    }

    @Override
    public void deleteExaminationById(Integer id) {
        examinationDao.deleteExaminationTaskByExaminationInfoId(id);
        examinationDao.deleteExaminationInfoById(id);
    }

    @Override
    public int addExamination(String examinationName, String taskIds, Integer type) {
        ExaminationInfo examinationInfo = new ExaminationInfo();
        examinationInfo.setExaminationName(examinationName);
        examinationInfo.setExaminationType(type);
        examinationInfo.setEstablishData(DateUtil.getSystemDate("yyyy-MM-dd HH:mm:ss"));
        Integer examinationId = examinationDao.addExamination(examinationInfo);
        String[] taskInfoArray = taskIds.split("t");
        ExaminationTask examinationTask = new ExaminationTask(examinationId);
        for(int i = 0; i<taskInfoArray.length;i++){
            String[] taskInfo = taskInfoArray[i].split(",");
            examinationTask.setLibraryTaskId(Integer.parseInt(taskInfo[0]));
            examinationTask.setTaskScore(Integer.parseInt(taskInfo[1]));
            examinationDao.addExaminationTask(examinationTask);
        }

        return examinationId;
    }

    @Override
    public ExaminationInfo findExaminationById(int taskId) {
        ExaminationInfo examinationInfo = examinationDao.findExaminationById(taskId);
        examinationInfo.setShowExaminationType(classifyService.findTypeValuesById(examinationInfo.getExaminationType()).getTypeValue());
        return examinationInfo;
    }

    @Override
    public ExaminationInfo analyzeExamination(ExaminationInfo examinationInfo) {
        return null;
    }

    @Override
    public void editExamination(ExaminationInfo examinationInfo) {
        examinationDao.editExamination(examinationInfo);
    }

    @Override
    public List<LibraryTask> addExaminationTaskToCache(String ids) {
        List<LibraryTask> libraryTasks = new ArrayList<LibraryTask>();
        String[] idList = ids.split(",");
        for(int i = 0; i< idList.length; i++){
            libraryTasks.add(libraryService.findLibraryTaskById(Integer.parseInt(idList[i])));
        }
        return libraryTasks;
    }

    @Override
    public List<LibraryTask> findTaskByExaminationInfoId(Integer id) {
        List<ExaminationTask> examinationTasks = examinationDao.findExaminationTaskByExaminationInfoId(id);
        List<LibraryTask> libraryTasks = new ArrayList<LibraryTask>();
        for(ExaminationTask examinationTask:examinationTasks){
            LibraryTask libraryTask = libraryService.findLibraryTaskById(examinationTask.getLibraryTaskId());
            libraryTask.setTaskScore(examinationTask.getTaskScore());
            libraryTasks.add(libraryTask);
        }
        return libraryTasks;
    }

    @Override
    public List<ExaminationInfo> findAllExaminationList() {
        return examinationDao.findAllExaminationList();
    }
}
